/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator.magicindicator;

import ohos.agp.components.Text;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;


/**
 * 带文本的指示器标题
 *
 * @since 2021-08-10
 */
public class SimplePagerTitleView extends Text implements ImeasurablePagerTitleView {
    private static final int NUM10 = 10;
    private static final int NUM16 = 16;
    private static final int NUM2 = 2;
    /**
     * 选择的颜色
     */
    protected int mSelectedColor;
    /**
     * 未选中的颜色
     */
    protected int mNormalColor;

    /**
     * 构造
     *
     * @param context context
     */
    public SimplePagerTitleView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        setTextAlignment(TextAlignment.CENTER);
        int padding = UiUtil.vp2px(context, NUM10);

        setPadding(padding, 0, padding, 0);
        setTextSize(NUM16, TextSizeType.FP);
        setMultipleLine(false);
        setTruncationMode(TruncationMode.ELLIPSIS_AT_END);

    }

    @Override
    public void onSelected(int index, int totalCount) {
        setTextColor(new Color(mSelectedColor));
    }

    @Override
    public void onDeselected(int index, int totalCount) {
        setTextColor(new Color(mNormalColor));
    }

    @Override
    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
    }

    @Override
    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
    }

    @Override
    public int getContentLeft() {
        String longestString = "";
        if (getText().toString().contains("\n")) {
            String[] brokenStrings = getText().toString().split("\\n");
            for (String each : brokenStrings) {
                if (each.length() > longestString.length()) {
                    longestString = each;
                }
            }
        } else {
            longestString = getText().toString();
        }

        Rect bound = new Paint().getTextBounds(longestString);
        int contentWidth = bound.getWidth();
        return getLeft() + getWidth() / NUM2 - contentWidth / NUM2;
    }

    @Override
    public int getContentTop() {
        Paint.FontMetrics metrics = new Paint().getFontMetrics();
        float contentHeight = metrics.bottom - metrics.top;
        return getHeight() / NUM2 - (int)contentHeight / NUM2;
    }

    @Override
    public int getContentRight() {
        String longestString = "";
        if (getText().toString().contains("\n")) {
            String[] brokenStrings = getText().toString().split("\\n");
            for (String each : brokenStrings) {
                if (each.length() > longestString.length()) {
                    longestString = each;
                }
            }
        } else {
            longestString = getText().toString();
        }
        Rect bound = new Paint().getTextBounds(longestString);
        int contentWidth = bound.getWidth();
        return getLeft() + getWidth() / NUM2 + contentWidth / NUM2;
    }

    @Override
    public int getContentBottom() {
        Paint.FontMetrics metrics = new Paint().getFontMetrics();
        float contentHeight = metrics.bottom - metrics.top;
        return getHeight() / NUM2 + (int)contentHeight / NUM2;
    }

    /**
     * getSelectedColor
     *
     * @return mSelectedColor
     */
    public int getSelectedColor() {
        return mSelectedColor;
    }

    /**
     * setSelectedColor
     *
     * @param selectedColor selectedColor
     */
    public void setSelectedColor(int selectedColor) {
        mSelectedColor = selectedColor;
    }

    /**
     * getNormalColor
     *
     * @return mNormalColor
     */
    public int getNormalColor() {
        return mNormalColor;
    }

    /**
     * setNormalColor
     *
     * @param normalColor mNormalColor
     */
    public void setNormalColor(int normalColor) {
        mNormalColor = normalColor;
    }
}
